//
//  EnterpriseRegistrationViewController.swift
//  swiftPhoto
//
//  Created by zetafin on 2019/3/14.
//  Copyright © 2019 赵宏亚. All rights reserved.
//

import Foundation

import UIKit

class EnterpriseRegistrationViewController:UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        addViews()
        
    }
    
    func addViews(){
        
        self.view .backgroundColor=UIColor.green
        
//        let btn = UIButton(frame: CGRectMake(30,80,50,50))
        let btn = UIButton(frame: CGRect(x: 30, y: 80, width: 50, height: 50))
        
        btn.setTitle("相册", for:.normal)
        
        btn.backgroundColor=UIColor.red
        
        btn.addTarget(self, action:Selector(("openAlbum")), for: .touchUpInside)
        
        self.view .addSubview(btn)
        
        
        
        
        
        let btn1 = UIButton(frame: CGRect(x: 90, y: 80, width: 50, height: 50))
        
        btn1.setTitle("相机", for:.normal)
        
        btn1.backgroundColor=UIColor.red
        
        btn1.addTarget(self, action:Selector(("demoCameera")), for: .touchUpInside)
        
        self.view .addSubview(btn1)
        
        
        
    }
    
    func demoCameera(){
        
        openCamera()
        
        
        
    }
    
    func demoClicked(){
        
        openAlbum()
        
    }
    
    //打开相册
    
    func openAlbum(){
        
        //判断设置是否支持图片库
        
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            
            //初始化图片控制器
            
            let picker = UIImagePickerController()
            
            //设置代理
            
            picker.delegate = self
            
            //指定图片控制器类型
            
            picker.sourceType = UIImagePickerController.SourceType.photoLibrary
            
            //设置是否允许编辑
            
            //            picker.allowsEditing = editSwitch.on
            
            //弹出控制器，显示界面
            
            self.present(picker, animated:true, completion: {
                
                () -> Void in
                
            })
            
        } else {
            
            print("读取相册错误")
            
        }
        
    }
    
    
    
    func openCamera(){
        
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            
            
            
            //创建图片控制器
            
            let picker = UIImagePickerController()
            
            //设置代理
            
            picker.delegate = self
            
            //设置来源
            
            picker.sourceType = UIImagePickerController.SourceType.camera
            
            //允许编辑
            
            picker.allowsEditing = true
            
            //打开相机
            
            self.present(picker, animated:true, completion: { () -> Void in

            })
            
        } else {
            
            debugPrint("找不到相机")
            
        }
        
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        //查看info对象
        
        print(info)
        
        //获取选择的原图
        
        guard let image = info[.editedImage] as? UIImage else {
            print("No image found")
            return
        }
        
        
        print("image ****** \(image.size)")
        
        //图片控制器退出
        
        picker.dismiss(animated: true, completion: {
            
            () -> Void in
            
        })
    }
    
}
